package com.boatAnchorAssistant.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boatAnchorAssistant.pojo.entity.Notice;
import com.boatAnchorAssistant.pojo.param.notice.*;
import com.boatAnchorAssistant.pojo.vo.notice.*;
import com.boatAnchorAssistant.pojo.dto.notice.*;
import com.boatAnchorAssistant.service.repository.in.NoticeRepository;
import com.boatAnchorAssistant.service.service.in.NoticeService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：通知服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    private final NoticeRepository noticeRepository;

    public NoticeServiceImpl(NoticeRepository noticeRepository) {
        this.noticeRepository = noticeRepository;
    }


    /**
     * 创建通知
     *
     * @param param
     */
    @Override
    public void createNotice(CreateNoticeParam param) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(param, notice);
        Long id = IdGenerator.SNOW.generate();
        notice.setId(id);
        noticeRepository.create(notice);
    }

    /**
     * 修改通知
     *
     * @param param
     */
    @Override
    public void editNotice(EditNoticeParam param) {
        Long id = param.getId();
        Notice notice = noticeRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, notice);
        noticeRepository.edit(notice);
    }

    /**
     * 移除通知
     *
     * @param id
     */
    @Override
    public void removeNotice(Long id) {
        noticeRepository.removeById(id);
    }

    /**
     * 启动/禁用通知
     *
     * @param id
     */
    @Override
    public void enabledNotice(Long id) {
        Notice notice = noticeRepository.getByIdCheck(id);
        notice.setIsDisable(!notice.getIsDisable());
        noticeRepository.edit(notice);
    }

    /**
     * 获取通知详情
     *
     * @param id
     * @return
     */
    @Override
    public GetNoticeVo getNotice(Long id) {
        Notice notice = noticeRepository.getByIdCheck(id);
        GetNoticeVo vo = new GetNoticeVo();
        BeanUtils.copyProperties(notice, vo);
        return vo;
    }

    /**
     * 获取最近的一条通知
     * @return
     */
    @Override
    public GetNoticeVo selectOneNotice() {
        GetNoticeVo vo = new GetNoticeVo();
        List<Notice> notices = noticeRepository.getBaseMapper()
                .selectList(new LambdaQueryWrapper<Notice>()
                        .eq(Notice::getIsDisable, false)
                        .orderByDesc(Notice::getCreateTime)
                        .last("limit 1"));
        if (notices.size() > 0) {
            BeanUtils.copyProperties(notices.get(0), vo);
        }
        return vo;
    }

    /**
     * 获取通知 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetNoticeListVo> getNoticeList(GetNoticeListParam param) {

        SelectNoticeDto dto = new SelectNoticeDto();
        BeanUtils.copyProperties(param, dto);
        Page<Notice> page = noticeRepository.selectNotice(dto);
        List<Notice> noticeList = page.getRecords();
        List<GetNoticeListVo> list = new ArrayList<>();
        for (Notice notice : noticeList) {
            GetNoticeListVo vo = new GetNoticeListVo();
            BeanUtils.copyProperties(notice, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取通知下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetNoticeDownListVo> getNoticeDownList(GetNoticeDownListParam param) {
        Notice po = new Notice();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<Notice> noticeList = noticeRepository.getListByEntity(po);

        List<GetNoticeDownListVo> list = new ArrayList<>();
        for (Notice notice : noticeList) {
            GetNoticeDownListVo vo = new GetNoticeDownListVo();
            BeanUtils.copyProperties(notice, vo);
            list.add(vo);
        }
        return list;
    }


}



